<?php
header("Content-Type:text/html;charset=utf-8");
require("../inc/common.inc.php");
// $wxmxopenid = $_COOKIE["mxadminwx"]["wxmxopenid"];
if(empty($_COOKIE['userid']) || $_COOKIE['userid']=="" || empty($_COOKIE['username']) || $_COOKIE['username']==""){
	$result['error'] = "您需要登录后才可以操作。";
	echo json_encode($result);
	exit();
}else{
	$mxuserid = $_COOKIE['userid'];
	$mxusername = $_COOKIE['username'];
	$mxnickname = $_COOKIE['nickname'];
	
}

$result['error']="";

$vpp_user_info=$db->getinfo("select * from mx_app_user where id='$mxuserid' limit 1");
if (!is_array($vpp_user_info) and $result['error']==""){
    $result['error'] = "非法用户,不能操作";
}

//判断是否有大转盘
$sql= "select * from mx_vpp_caipiao where type='1' and isok='1' and id='".$id."' order by orderid asc,createdate desc,id desc limit 1";
$dzpinfo= $db -> getinfo($sql);
if (!is_array($dzpinfo) and $result['error']==""){
    $result['error'] = "非法访问";
}
if($dzpinfo["startdate"]>date("Y-m-d") and $result['error']=="" ){
    $result['error'] = "大转盘活动还没有开始，开始时间为：".$dzpinfo["startdate"];
}
if($dzpinfo["enddate"]<date("Y-m-d") and $result['error']=="" ){
    $result['error'] = "大转盘活动已经结束";
}
$mxUserInfo = $db->getinfo("select * from mx_app_user where id=$mxuserid");
if($mxUserInfo['integral'] < $dzpinfo['needintegral']){
	$result['error'] = "您的积分不足";
}

$sql   = "select * from mx_vpp_caipiao_prize where isok='1' and cpid = '".$dzpinfo["id"]."'";
$query = $db -> excu($sql);
$prize_arr ="";
if ($db->num_rows($query)>0){
while ($rsdb  = $db -> fetch_array($query)) {
	//判断是否还有次奖品
	$havezhong=$db->listcount("mx_vpp_lingqu_cp"," and cpid='$dzpinfo[id]' and jxid='$rsdb[id]'");
	if ($rsdb["num"]>$havezhong){
		$prize_arr[] = array(
			'id'        => $rsdb['id'],
			'min'       => explode(',',$rsdb['range_min']),
			'max'       => explode(',',$rsdb['range_max']),
			'prize_str' => $rsdb['name'],
			'description' => $rsdb['description'],
			'v'         => $rsdb['gailv'],
			'isshowjp' => $rsdb['isshowjps'],
		);
	}
}
}
/*$prize_arr = array( 
    '0' => array('id'=>1,'min'=>1,'max'=>29,'prize_str'=>'一等奖','v'=>1), 
    '1' => array('id'=>2,'min'=>302,'max'=>328,'prize_str'=>'二等奖','v'=>2), 
    '2' => array('id'=>3,'min'=>242,'max'=>268,'prize_str'=>'三等奖','v'=>5), 
    '3' => array('id'=>4,'min'=>182,'max'=>208,'prize_str'=>'四等奖','v'=>7), 
    '4' => array('id'=>5,'min'=>122,'max'=>148,'prize_str'=>'五等奖','v'=>10), 
    '5' => array('id'=>6,'min'=>62,'max'=>88,'prize_str'=>'六等奖','v'=>25), 
    '6' => array('id'=>7,'min'=>array(32,92,152,212,272,332), 'max'=>array(58,118,178,238,298,358),'prize_str'=>'七等奖','v'=>50) 
); 
*/
if ($havezhong>=$dzpinfo["zongnum"] and $result['error']=="" ){
	$result['error'] = "奖品已经全部抽完，此次活动已经自动结束";
}


function getRand($proArr) { 
    $result = ''; 
 
    //概率数组的总概率精度 
    $proSum = array_sum($proArr); 
 
    //概率数组循环 
    foreach ($proArr as $key => $proCur) { 
        $randNum = mt_rand(1, $proSum); 
        if ($randNum <= $proCur) { 
            $result = $key; 
            break; 
        } else { 
            $proSum -= $proCur; 
        } 
    } 
    unset ($proArr); 
 
    return $result; 
}


/*foreach ($prize_arr as $key => $val) { 
    $arr[$val['id']] = $val['v']; 
} */
if (is_array($prize_arr)){
	foreach ($prize_arr as $key => $val) { 
		$arr[] = $val['v']; 
	}
	$rid = getRand($arr); //根据概率获取奖项id
}
else{
	$result['error'] = "奖品已经全部抽完，此次活动已经自动结束";
}


$rid = getRand($arr); //根据概率获取奖项id

//抽奖记录
$todayhavezhong=$db->listcount("mx_vpp_lingqu_cp"," and cpid='$dzpinfo[id]' and userid='$vpp_user_info[id]' and createdate>='".date("Y-m-d")." 00:00:00' and createdate<='".date("Y-m-d")." 23:59:59'");

if ($todayhavezhong>=$dzpinfo['usernum'] and $result['error']=="") {
	$result['error'] = "对不起，您今天的抽奖次数已经全部用完";
}


if($result['error']==""){
	$res = $prize_arr[$rid]; //中奖项 
	
	$min = $res['min']; 
	$max = $res['max']; 
	
	$i = mt_rand(0,(count($min)-1)); 
	$result['angle'] = mt_rand($min[$i],$max[$i]); 
	
	$result['prize']=$res["description"]; 
	
	//获得奖品
	$yhqcode=date("YmdHis").rand(1000,9999);
	//判断优惠券号是否存在
	$yhqhave=$db->getinfo("select * from mx_vpp_lingqu_cp where cpcode='$yhqcode' ");
	if (is_array($yhqhave)){
		$result['error'] = "加载失败，请重新点击抽奖";
	}
	$files=array(
		"cpid"       =>$dzpinfo["id"],
		"jxid"       =>$res["id"],
		"cpcode"     =>$yhqcode,
		"cptitle"    =>$dzpinfo["name"],
		"jxtitle"    =>$res["prize_str"],
		"userid"     =>$vpp_user_info["id"],
		"num"        =>1,
		"isshowjp"   =>$res["isshowjp"],
		"createdate" =>date("Y-m-d H:i:s"),
		"type"       =>1,
	);
	$db->exe_insert("mx_vpp_lingqu_cp",$files);
	if ($result['error']==""){
		addIntegral($dzpinfo['needintegral'], $mxuserid, "玩大转盘",2);
		$result['error']=0;
	}
}
echo json_encode($result); 
?>














